app.controller('datastoreCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal', '$stateParams', '$state', function ($rootScope, $scope, httpLoad, $modal, $stateParams, $state) {
    $rootScope.moduleTitle = '资源管理 > 存储备份';
    $rootScope.link = '/statics/css/tpl-card.css';
    sessionStorage.removeItem('resourcetabLocation');sessionStorage.removeItem('networktabLocation');

    var flag = sessionStorage.getItem('datastoretabLocation')||1;
    switch (flag/1){
        case 1:
            $scope.isStorage = true;
            break;
    }
    sessionStorage.setItem('datastoretabLocation','1');
}]);
app.controller('StorageCtrl', ['$rootScope', '$scope', 'httpLoad', '$modal','$timeout', '$stateParams', '$state', function ($rootScope, $scope, httpLoad, $modal,$timeout, $stateParams, $state) {
    $scope.param = {
        page: 1,
        rows: 10
    };
    $scope.showDetail = false;
    //详情机房列表
    if ($stateParams.id) {
        $rootScope.moduleTitle = '资源管理 > 机柜详情';
        $scope.param.params = angular.toJson([{param: {rackId: $stateParams.id}, sign: 'EQ'}])
    }
    //获取服务器列表数据
    $scope.getList = function (page) {
        $scope.showDetail = false;
        $scope.operation = {
            'isBatch1' : true,
            'selectAll1':false
        };
        $scope.param.page = page || $scope.param.page;
        httpLoad.loadData({
            url: '/device/storage/list',
            data: $scope.param,
            noParam: true,
            success: function (data) {
                if (data.success) {
                    $scope.serverListData = data.data.rows;
                    $scope.totalCount = data.data.total;
                    if(data.data.rows.length==0) $scope.isImageData = true;
                    else $scope.isImageData = false;
                }else $scope.isImageData = true;
            }
        });
    };
    //搜索
    $scope.search = function () {
        //对参数进行处理，去除空参数
        var toObjFormat = function (obj) {
            for (var a in obj) {
                if (obj[a] == "") delete obj[a];
            }
            return obj;
        }
        var params = [];
        var param1 = toObjFormat({
            name: $scope.name,
            serviceIp:$scope.serviceIp
        });
        var param2 = {};
        if ($stateParams.id) {
            param2.rackId = $stateParams.id;
        };
        if (angular.toJson(param1).length > 2) params.push({param: param1, sign: 'LK'});
        if (angular.toJson(param2).length > 2) params.push({param: param2, sign: 'EQ'});
        $scope.param = {
            page: 1,
            rows: 10,
            params: angular.toJson(params)
        }
        $scope.getList();
    }
    //重置搜索条件
    $scope.reset = function () {
        var obj = ['name','tenantName','manageIp'];
        angular.forEach(obj, function (data) {
            $scope[data] = '';
        })
    };
    //批量操作
    $scope.operation = {};
    $scope.operation.isBatch1 = true;$scope.operation.selectAll1 = false;
    $scope.chooseAll1 = function(){
        $scope.operation.isBatch1 = !$scope.operation.selectAll1;
        $scope.serverListData.forEach(function(item){
            item.isSelect = $scope.operation.selectAll1;
        });
    };
    $scope.choose1 = function(){
        var a = 0,b=0;
        $scope.serverListData.forEach(function(item){
            if(item.isSelect==true) a++;
            else b=1;
        });
        if(a>=1) $scope.operation.isBatch1 = false;
        else $scope.operation.isBatch1 = true;
        if(b==1) $scope.operation.selectAll1 = false;
        else $scope.operation.selectAll1 = true;
    };
    $scope.batch1 = function($event,value){
        $event.stopPropagation();
        $scope.operationData = '';var operationData = '';
        $scope.serverListData.forEach(function(item){
            if(item.isSelect==true) operationData += ','+item.id;
        });
        $scope.operationData = operationData.slice(1);
        switch (value){
            case 1:
            case 2:
                $scope.commendAction({'id':$scope.operationData,'batchUrl':1},value);
                break;
            case 3:
                $scope.delServer({"id":$scope.operationData,"batchUrl":1});
                break;
        }
    };
    //启动关闭
    $scope.commendAction = function (row,flag) {
        var tip,url,text;
        if(flag == 2){     //批量操作
            if(row.batchUrl&&row.batchUrl==1){
                tip = '确定要批量停止存储设备？';url = "/device/storage/batchStop";text = '存储设备批量停止成功';
            }else{
                tip = '确定要停止存储设备？';url = "/device/storage/stop";text = '存储设备停止成功';
            }
        }else{
            if(row.batchUrl&&row.batchUrl==1){
                tip = "确定要批量启动存储设备？";url="/device/storage/batchStart";text = '存储设备批量启动成功';
            }else{
                tip = "确定要启动存储设备？";url="/device/storage/start";text = '存储设备启动成功';
            }
        }
        var modalInstance = $modal.open({
            templateUrl: '/statics/tpl/assets/resource/delModal.html',
            controller: 'delModalCtrl',
            backdrop: 'static',
            resolve: {
                tip: function () {
                    return tip;
                },
                btnList: function () {
                    return [{name: '确定', type: 'btn-info'}, {name: '取消', type: 'btn-cancel'}];
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.operation.isBatch1 = true;$scope.operation.selectAll1 = false;
            angular.forEach($scope.serverListData, function (data, index) {
                data.isSelect = false;
            });
            httpLoad.loadData({
                url: url,
                data: {
                    id: row.id
                },
                success: function (data) {
                    if (data.success) {
                        $scope.pop(text);
                        if (flag == 1) {
                            row.status = 'RUNNING';
                        } else if(flag == 2) {
                            row.status = 'STOPPED';
                        };
                    }
                }
            });
        });
    }
    //删除
    $scope.delServer = function (row) {
        var modalInstance = $modal.open({
            templateUrl: '/statics/tpl/assets/infrastructure/storage/delModal.html',
            controller: 'delStorageModalCtrl',
            backdrop: 'static',
            resolve: {
                id: function () {
                    return row.id;
                },
                batchUrl:function(){
                    return row.batchUrl;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.getList();
            //$scope.rackDetail.storages -= 1;
        });
    };
    //跳转
    $scope.goAction = function (flag,id) {
        //if(flag == 1) id = $stateParams.id;
        $state.go('app.assets.addstorage', {flag:flag+3,id: id})
    };
    //返回
    $scope.goBack = function(){
        $scope.isActive = false;
        $timeout(function() {
            $scope.showDetail = false;
        }, 200);
    };
    //四级联动加载数据
    $scope.loadSelectData = function (dcId,roomId,rackId) {//flag用来判断编辑还是选择，编辑时不清空id
        //获取数据中心列表数据
        httpLoad.loadData({
            url: '/dc/list',
            noParam: true,
            success: function (data) {
                if (data.success) {
                    $scope.dataCenterData = data.data.rows;
                    $scope.dataCenterData.forEach(function(item){
                        if(item.id == dcId) $scope.itemData.dcName = item.name;
                        return;
                    });
                }
            }
        });
        httpLoad.loadData({
            url: '/room/list',
            noParam: true,
            success: function (data) {
                if (data.success) {
                    $scope.roomData = data.data.rows;
                    $scope.roomData.forEach(function(item){
                        if(item.id == roomId) $scope.itemData.roomName = item.name;
                        return;
                    });
                }
            }
        });
        httpLoad.loadData({
            url: '/rack/list',
            noParam: true,
            success: function (data) {
                if (data.success) {
                    $scope.cabinetData = data.data.rows;
                    $scope.cabinetData.forEach(function(item){
                        if(item.id == rackId) $scope.itemData.rackName = item.name;
                        return;
                    });
                }
            }
        });
    };
    //跳转详情页
    $scope.detail = function ($event,id) {
        $event.stopPropagation();
        //初始化默认值
        $scope.itemData = {
            type: 'PC',
            environment: 'DEVELOP'
        };
        var initDetailCreate = function () {
            $scope.disableSelect = true;
            httpLoad.loadData({
                url: '/rack/detail',
                method: 'GET',
                data: {id: id},
                success: function (data) {
                    if (data.success) {
                        var data = data.data;
                        $scope.itemData.dcId = data.dcId;
                        $scope.itemData.roomId = data.roomId;
                        $scope.itemData.rackId = data.id;
                        $scope.loadSelectData($scope.itemData.dcId,$scope.itemData.roomId,$scope.itemData.rackId);
                    }
                }
            });
        };
        var initDetailEdit = function (id) {
            httpLoad.loadData({
                url: '/device/storage/detail',
                method: 'GET',
                data: {id: id},
                success: function (data) {
                    if (data.success) {
                        $scope.itemData = data.data;
                        $scope.showDetail = $scope.isActive = true;
                        if ($scope.itemData.serviceStart) {
                            $scope.date = $scope.itemData.serviceStart + ' - ' + $scope.itemData.serviceEnd;
                        }
                    }
                }
            });
        };
        $rootScope.moduleTitle = '资源管理 > 存储设备详情';
        $scope.modalName = '存储设备详情';
        initDetailEdit(id);
        $scope.isDetail = true;
    };
}])
//删除
app.controller('delStorageModalCtrl', ['$scope', '$modalInstance', 'httpLoad', 'id','batchUrl',
    function ($scope, $modalInstance, httpLoad, id,batchUrl) {
        var text,url;
        if(batchUrl&&batchUrl==1){
            text = '存储设备批量删除成功';
            url = '/device/storage/batchRemove';
            $scope.text = '确定要批量删除存储设备吗？';
        }else{
            text = '存储设备删除成功';
            url = '/device/storage/remove';
            $scope.text = '确定要删除存储设备吗？';
        }

        $scope.ok = function () {
            httpLoad.loadData({
                url: url,
                data: {id: id},
                success: function (data) {
                    if (data.success) {
                        $scope.pop(text);
                        $modalInstance.close();
                    }
                }
            });
        }
        $scope.cancle = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);